var chessDD = function(id, sGroup, config) {
    chessDD.superclass.constructor.call(this, id, sGroup, config);
};

YAHOO.extend(chessDD, YAHOO.util.DD, {
    startDrag: function(x, y) {
        clearTimeout(t);
        var el=this.getEl();
        var style = el.style;
        this.origZ = style.zIndex;
        this.origX = Math.round((YAHOO.util.Dom.getXY(el)[0] - 25) / 50 - 1);
        this.origY = Math.round((YAHOO.util.Dom.getXY(el)[1] - 25) / 50 - 1);
        style.zIndex = 999;
    },

    endDrag: function(e) {
        var el=this.getEl();
        //this.getEl().style.zIndex = 50;//this.origZ;
        el.style.zIndex = this.origZ;
        if (!YAHOO.util.Dom.getXY(el)){
            el.style.top = this.origY * 50 - 25;
            el.style.left = this.origX * 50 - 25;
            return;
        }
        alert (YAHOO.util.Dom.getXY(el));
        var x = Math.round((YAHOO.util.Dom.getXY(el)[0] - 25) / 50 - 1);
        var y = Math.round((YAHOO.util.Dom.getXY(el)[1] - 25) / 50 - 1);
        if (x < 1 || y < 1 || x > 9 || y > 10) {
            x = this.origX;
            y = this.origY;
            return;
        }
        el.style.top = y * 50 - 25;
        el.style.left = x * 50 - 25;
        alert ( x+"/"+y);
        if (color == 'red') {
            y = 11 - y;
            this.origY = 11 - this.origY;
        }
        var sUrl = "/chess/game/addMove/" + gameid + "?moveFromX=" + this.origX + "&moveFromY=" + this.origY + "&moveToX=" + x + "&moveToY=" + y
        //alert(sUrl);
        var handleF = function(o) {
            alert("error");
        }
        var handleS = function(o) {
            //alert("success");
            drawpiece();
        }
        var a = {
            success: handleS,
            faulure: handleF  ,
            timeout: 3000

        }
        var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, a);
        repeat();
    }

});


function drawpiece(e) {
    var callbacks = {
        success : function (o) {
            // Process the JSON data returned from the server
            var pieces = document.getElementById('pieces');
            pieces.innerHTML = "";
            var piecelist = [];
            try {
                piecelist = YAHOO.lang.JSON.parse(o.responseText);
            }
            catch (x) {
                alert("JSON Parse failed!");
                return;
            }
            for (var i = 0, len = piecelist.length; i < len; ++i) {
                var piece = piecelist[i];
                var p = document.createElement("div");
                p.id = "p" + i;
                p.setAttribute("class", "piece");
                p.style.background = "url(/chess/images/pieces/" + piece.type + "_" + piece.color + ".png) 0 0 no-repeat";
                p.style.cursor = "hand";
                if (!piece.alive) p.style.display = "none";
                if (color == 'red') {
                    piece.y = 11 - piece.y;
                }
                p.style.top = piece.y * 50 - 25 + "px";
                p.style.left = piece.x * 50 - 25 + "px";
                pieces.appendChild(p);
                new chessDD("p" + i);
            }
            //dd();

        },

        failure : function (o) {
            if (!YAHOO.util.Connect.isCallInProgress(o)) {
                alert("Async call failed!");
            }
        },

        timeout : 3000
    }

    // Make the call to the server for JSON data
    var Url = "/chess/game/showBoard/" + gameid;
    //alert (Url);
    YAHOO.util.Connect.asyncRequest('GET', "/chess/game/showBoard/" + gameid, callbacks);
}

